<template>
<!-- 龙虎榜数据 -->
    <div class="block" v-loading="isLoad">
        <el-row class="block-item">
            <exDate v-model="date" label="选择日期" size="mini" value-format="yyyy-MM-dd"></exDate>
            <exSwitch v-model="onlyUp" label="只保留上涨的" style="margin-top:8px;"></exSwitch>
            <el-button type="primary" @click="query" size="mini" style="height:28px;margin-top:4px">查询</el-button>
            <el-button type="warning" @click="saveData" size="mini" style="height:28px;margin-top:4px">存入数据库</el-button>
            <el-link type="primary" target="_blank" href="http://data.eastmoney.com/stock/lhb.html" class="ml20">取数链接</el-link>
        </el-row>
        <exPubTable ref="curTable" :cfg.sync="cfg" :exData="dataList" @getDataList="queryDataList">
            <exColumn label="名称" width="100">
                <template slot-scope="s">
                    <klinePic :code="s.row.SCode" :name="s.row.SName"></klinePic>
                </template>
            </exColumn>
        </exPubTable>
    </div>
</template>



<script>
import Vue from "vue";

import klinePic from "../trade/comp/klinePic"

export default {
    components:{
        klinePic,
    },
    data() {
        var that = this;
        return {
            isLoad: false,
            date : this.$dayjs().format('YYYY-MM-DD'),//this.$dayjs().subtract("day",1).format('YYYY-MM-DD')
            dataList:[],
            onlyUp : false,
            cfg:{
                usePage:false,
                showIndex: true,
                btns:[
                    {name:"比对",handle:({row})=>{
                        this.addComp(row);
                    }},
                ],
                columns2: [
                    {prop:"SCode",label:"代码",width:80,sortable:true},
                    {prop:"Tdate",label:"日期",width:100,sortable:true},
                    {prop:"Chgradio",label:"当日涨幅",width:110,sortable:true,formatter:this.fixed2,endStr:"%",align:"right",getColor(s){return that.getRateColor(s.row.Chgradio)}},
                    {prop:"ClosePrice",label:"当日价格",width:110,sortable:true,align:"right",getColor(s){return that.getRateColor(s.row.Chgradio)}},
                    {prop:"JmMoney",label:"净买入",width:110,sortable:true,formatter:this.moneyFixedFormat},
                    {prop:"BRate",label:"买入占比",width:110,sortable:true,align:"right",endStr:"%",tips:"净买入/总成交"},
                    {prop:"Bmoney",label:"总买入",width:110,sortable:true,formatter:this.moneyFixedFormat},
                    {prop:"Smoney",label:"总卖出",width:110,sortable:true,formatter:this.moneyFixedFormat},
                    // {prop:"SCode",label:"连榜次数",width:80,sortable:true},
                    // {prop:"SCode",label:"近1年上榜次数",width:80,sortable:true},
                    // {prop:"SCode",label:"所属概念",width:80,sortable:true},
                    {prop:"Ctypedes",label:"上榜原因",width:220,sortable:true,"show-overflow-tooltip":true},
                ],
            }
        }
    },
    computed:{
    },
    provide () {
      return {
      }
    },

    mounted() {

    },

    methods: {
        addComp(item){
            window.$vue.addCompItem({code:item.SCode,name:item.SName});
        },
        query(){
            if(this.$refs.curTable) this.$refs.curTable.reQuery();
        },
        queryDataList(){
            this.isLoad = true;
            this.$axios.get(`http://data.eastmoney.com/DataCenter_V3/stock2016/TradeDetail/pagesize=200,page=1,sortRule=-1,sortType=,startDate=${this.date},endDate=${this.date},gpfw=0,js=var%20data_tab_1.html?rt=26839125`)
            .then((res) => {
                this.isLoad = false;
                if (res.data) {
                    var data = res.data.replace("var data_tab_1=","")
                    data = JSON.parse(data)
                    this.dataList = [];
                    var tmpmap = {};
                    data.data.map(item=>{
                        if(tmpmap[item.SCode]) return;
                        if(this.onlyUp && item.Chgradio*1<=0){
                            return;
                        }
                        tmpmap[item.SCode] = item.SCode;
                        item.Tdate = item.Tdate.replace(/[^0-9]*/g,"")
                        item.Chgradio = item.Chgradio*1;
                        item.JmMoney = item.JmMoney*1;
                        item.Bmoney = item.Bmoney*1;
                        item.Smoney = item.Smoney*1;
                        item.BRate = (item.JmMoney *100/item.Turnover).toFixed(2)*1
                        item.BRate = Math.abs(item.BRate);
                        this.dataList.push(item);
                    })
                } else {
                    this.$message.error(res.data.message);
                }
            })
        },
        saveData(){
            var list = this.$refs.curTable.getCurDataList();

            list.map(item=>{

                var data= {
                    id: item.Tdate+""+item.SCode,
                    day: item.Tdate,
                    code: item.SCode,
                    name: item.SName,
                    rise: item.Chgradio,
                    price: item.ClosePrice,
                    jmoney: item.JmMoney,
                    buyrate: item.BRate,
                    buymoney: item.Bmoney,
                    salemoney: item.Smoney,
                    reason : item.Ctypedes,
                    createtime:new Date(),
                }
                
                var opername = "insertLHData";
                this.registerListen(this,opername,(res)=>{
                    if(res.succ){
                        this.$message.success("插入成功")
                    }else{
                        this.$message.error("插入异常:"+res.err)
                    }
                });
                var param = {type:opername,name:opername,
                    objInfo:data};
                electron.ipcRenderer.send("doSql",param)

            })
                
        }
    }
    
}
</script>
